using Microsoft.EntityFrameworkCore;
using Rabbit.Infrastructure.Implement;
using Rabbit.Infrastructure.Interface;
using RabbitInfrastructure;
using System.Reflection;
using SkiaSharp;
using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

// 在这里初始化 RedisHelper
RabbitTraceability.API.Application.Utils.RedisHelper.Init(builder.Configuration);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
    o.IncludeXmlComments(AppContext.BaseDirectory+ "RabbitTraceability.API.xml", true);
});

//�н���ע��
builder.Services.AddMediatR(a=>a.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

//���ݿ�������
builder.Services.AddDbContext<MyDbContex>(a=>a.UseSqlServer(builder.Configuration.GetConnectionString("Conn")));

//�ִ��ӿ�ע��
builder.Services.AddScoped(typeof(IBaseRepository<>),typeof(BaseRepository<>));


// Program.cs
builder.Services.AddMemoryCache();

// 拼接 Redis 连接字符串
var redisSection = builder.Configuration.GetSection("Redis");
var host = redisSection["Host"];
var port = redisSection["Port"];
var password = redisSection["Password"];
var database = redisSection["Database"];
var connectTimeout = redisSection["ConnectTimeout"];
var responseTimeout = redisSection["ResponseTimeout"];

var redisConfig = $"{host}:{port},password={password},defaultDatabase={database},connectTimeout={connectTimeout},syncTimeout={responseTimeout}";

if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port) || string.IsNullOrWhiteSpace(password))
{
    throw new Exception("Redis配置不完整，请检查appsettings.json中的Redis节点。");
}

// 注册 Redis 单例
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
    ConnectionMultiplexer.Connect(redisConfig));


// 健康检查
builder.Services.AddHealthChecks().AddRedis(redisConfig, name: "redis");


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

//��������
app.UseCors(a=>a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

app.UseHttpsRedirection();

app.MapControllers();

app.Run();
